<?php


class Antz_Form extends Antz_Form_Component
{
    public $id;
    protected $blocks = array();
    protected $encType = '';
    protected $jsEvents = array();
    protected $intelliFormId='default';

    public function __construct($intelliFormId='default'){
        $id = 'form'.rand();
        $this->intelliFormId = $intelliFormId;
        $this->setId($id);
        $this->setMethod();
        $this->setAction(THIS_PAGE_URL);
    }

	/**
	 * Deprecated in favour of Antz::makeForm()
	 * Will disappear in v1.0
	 */
    public static function rapidForm($legend, $action=''){
        $form = Antz::newObject('Antz_Form');
        if($action!='') $form->setAction($action);
        $block = $form->add('full');
        $fs = $block->add($legend);
        return $fs;
    }

	public function setEncType($encType){
		$this->encType = $encType;
	}

    public function getEncType(){
    	if($this->encType == '') return '';
        return ' enctype="'.$this->encType.'" ';
    }

    public function setAction($url){
        $this->action = $url;
    }

    public function setMethod($method='post'){
        $this->method = $method;
    }

    public function setId($id){
        $this->id = $id;
    }

    public function getId(){
        return $this->id;
    }

    public function addJsEvent($event, $function){
        $this->jsEvents[$event] = $function;
    }

	/**
	 * Adds a block to the form, blocks hold fieldsets
	 * $colType = 'full' or 'double'
	 * $id = html id, optional
	 */
    public function add($colType, $id=''){
        if($id=='')$id = $this->id.'-block-'.rand();
        $key = count($this->blocks);
        $this->blocks[$key] = Antz::newObject('Antz_Form_Block', array($colType, $id));
        $this->blocks[$key]->formObj = $this;
        return $this->blocks[$key];
    }

    public function addCustomObject($obj){
        if(!method_exists($obj, 'render')) die('not defined');
        $this->blocks[] = $obj;
    }

    public function render(){
        $returnString = '<form method="'.$this->method.'" action="'.$this->action.'" '.
        	$this->getEncType().' id="'.$this->getId().'" '.$this->renderClasses().' '.
        	$this->renderParameters().'>';
        $returnString .= Antz_IntelliForm::seed($this->intelliFormId);
        foreach($this->blocks as $block){
            $returnString .= "\n  ".$block->render();
        };
        $returnString .= "\n".'</form>';
        return $returnString;
    }

    public function addSubmit($val = 'Submit changes >>'){
        $submitBlock = $this->add('full');
        $submitBlock->addClass('bold-submit');
        $fs = $submitBlock->add('');
        $fs->showBorder(false);
        $fs->addInput('submit', 'submitbtn', '&nbsp;', $val);
        return $submitBlock;
    }

    public function addJsSubmit($val = 'Submit changes >>', $question='', $callbackFunctions = '', $customFunction = ''){
        if($question == ''){
            $question = 'Are you sure you want to do this?';
        };
        $submitBlock = $this->add('full');
        $submitBlock->addClass('bold-submit');
        $fs = $submitBlock->add('');
        $fs->showBorder(false);
        $btn = $fs->addInput('submit', 'submit', '&nbsp;', $val);
        $btn->setId('submit');
        if($customFunction == ''){
            $this->addJsEvent('onsubmit', 'return AntzBase.confirmSubmit(\''.$question.'\', \''.$callbackFunctions['yes'].'\', \''.$callbackFunctions['no'].'\')');
        }else{
            $this->addJsEvent('onsubmit', 'return AntzBase.customConfirmSubmit(\''.$customFunction.'\', \''.$callbackFunctions['yes'].'\', \''.$callbackFunctions['no'].'\')');
        }
        return $submitBlock;
    }

    public function addConfirmCancel($lastPage, $confVal='Confirm >>', $cancelVal='<< Cancel'){
        $submitBlock = $this->add('full');
        $submitBlock->addClass('bold-submit');
        $fs = $submitBlock->add('');
        $fs->showBorder(false);
        $cancel = $fs->addInput('button', 'cancel', '', $cancelVal);
        $cancel->addJsEvent('onclick', 'window.location=\''.$lastPage.'\'');
        $cancel->showLabel(false);
        $cancel->setWrap(false);
        $submit = $fs->addInput('submit', 'submitbtn', '&nbsp;', $confVal);
        $submit->showLabel(false);
        $submit->setWrap(false);
        return $submitBlock;
    }

}
